package com.example.pm;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public abstract class BaseActivity extends Activity
{

    private SharedPreferences sp;
	private AlertDialog createUserDialog;
	private AlertDialog checkUserDialog;
	private boolean isFirst;
	private boolean isLogined;
	
	
	private roomService.MyBinder myBinder;
   //All abstract methods
	/*
   public abstract void initialize();
   public abstract void createRoom();
   public abstract void clearRoom();
   public abstract void refreshTalk();  */

	public abstract void mainView();
     //方法具体实现
	@Override
    protected void onCreate(Bundle savedInstanceState)
    {
	super.onCreate(savedInstanceState);
	if(Net.isNetworkAvailable(getApplicationContext())){//如果网络可以使用，则进行数据初始化工作
		initialize();   
	   }
	
	sp=getSharedPreferences("data",0);
	isFirst=sp.getBoolean("first",true);
	isLogined=sp.getBoolean("isLogined",false);
	if(isFirst){//first is true,then login room
	  createInfo();//create user information
	}
	else{
	 checkInfo();    //check user's information
	}
  }
	
private void initialize(){
		   //初始化数据，主要用于获取整个房间使用情况信息
		   //数据初始化主要使用到Service和多线程
	   Intent bindIntent=new Intent(this,roomService.class);
	   bindService(bindIntent, initconnection, BIND_AUTO_CREATE);

}
private ServiceConnection initconnection=new ServiceConnection(){

	@Override
	public void onServiceConnected(ComponentName name, IBinder service) {
		// TODO Auto-generated method stub
		myBinder=(roomService.MyBinder)service;
		myBinder.roomsInit();                                       //初始化房间信息			
			}

	@Override
	public void onServiceDisconnected(ComponentName arg0) {
		// TODO Auto-generated method stub
		
	}};

public void exitRoom(){
	      //退出房间，清除service一切信息
	     //删除服务器房间表格信息
	
}	   
public String getName(){
	if(!sp.getString("name","").equals("")){
		return sp.getString("name","");
	}
	return "noname";
}
public void createInfo()
	{
		// create user's information
		AlertDialog.Builder builder=new AlertDialog.Builder(this);
		LayoutInflater inflater=LayoutInflater.from(this);
		View view=inflater.inflate(R.layout.checker,null);
		final EditText username=(EditText)view.findViewById(R.id.username);
		final EditText password=(EditText)view.findViewById(R.id.password);
		Button ok=(Button)view.findViewById(R.id.ok);
		Button cancel=(Button)view.findViewById(R.id.cancel);
		ok.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v)
				{
					// TODO: Implement this method
					String name=username.getText().toString();
					String pw=password.getText().toString();
					if(name.trim().equals("")||pw.trim().equals("")){
						Toast.makeText(BaseActivity.this,"username or password is blank",Toast.LENGTH_SHORT).show();
						return;
					}
					
				SharedPreferences.Editor editor=sp.edit();
				editor.putString("name", name);
				editor.putString("password", pw);
			    editor.putBoolean("first",false);
			    editor.putBoolean("isLogined",true);
			    editor.commit();
					createUserDialog.dismiss();
					mainView();
				}
			});
	cancel.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v0)
			{
				// TODO: Implement this method
				if(isFirst){
					BaseActivity.this.finish();
				}
				
					createUserDialog.dismiss();
				
			}
		});
		builder.setTitle("建立用户");
		builder.setView(view);
		createUserDialog=builder.create();
		createUserDialog.show();
	}
   public void checkInfo(){
	   AlertDialog.Builder builder=new AlertDialog.Builder(this);
	   LayoutInflater inflater=LayoutInflater.from(this);
	   View view=inflater.inflate(R.layout.checker,null);
	   final EditText username=(EditText)view.findViewById(R.id.username);
	   final EditText password=(EditText)view.findViewById(R.id.password);
	   Button ok=(Button)view.findViewById(R.id.ok);
	   Button cancel=(Button)view.findViewById(R.id.cancel);
	   ok.setOnClickListener(new OnClickListener(){

			   @Override
			   public void onClick(View v)
			   {
				   // TODO: Implement this method
				   String name=username.getText().toString();
				   String pw=password.getText().toString();
				   if(name.trim().equals("")||pw.trim().equals("")){
					   Toast.makeText(BaseActivity.this,"用户名或密码为空",Toast.LENGTH_SHORT).show();
					   return;
				   }
				   
				   //check username and password
				   String savedUsername=sp.getString("name","");
				   String savedPassword=sp.getString("password","");
				   if(name.trim().equals(savedUsername)&&pw.trim().equals(savedPassword)){
					   sp.edit().putBoolean("isLogined",true).commit();
					   mainView();
					   checkUserDialog.dismiss();
				   }
				   else{
					   Toast.makeText(BaseActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
					   return;
				   }
			   }
		   });
	   cancel.setOnClickListener(new OnClickListener(){

			   @Override
			   public void onClick(View v0)
			   {
				   // TODO: Implement this method
				   checkUserDialog.dismiss();
				   BaseActivity.this.finish();
			   }
		   });
	   builder.setTitle("用户登录");
	   builder.setView(view);
	   checkUserDialog=builder.create();
	   checkUserDialog.show();   
   }

} 
